package com.casstime.ec.cloud.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
	
	@Bean(name="defaultDataSource",destroyMethod = "close")
	@Qualifier("defaultDataSource")
	@Primary
	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource defaultDataSource() {
		return DataSourceBuilder.create().build();
	}	
	
	@Bean(name="transactionManager")
	@Primary
    public PlatformTransactionManager defaultTransactionManager(@Qualifier("defaultDataSource") DataSource datasource) {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(datasource);
		transactionManager.setGlobalRollbackOnParticipationFailure(false);
		return transactionManager;
    }

}
